dojo.require("selfpass.module.User");

function initUser(){
	if(dijit.byId("initUserForm").validate()){
		dojo.xhrPost({
			url: '/service/doInit',
			handleAs: 'json',
			handle: function(data,args){
				if(data.message && data.message == "success"){
					showLogin();
				}else{
					showErrMsg("Init Failure!");
				}
			},
			form: 'initUserForm'
		});
	}else{
		 showErrMsg("Please check again, some value you want to submit is incorrect");
	}
}
function login(){
	if(dijit.byId("loginForm").validate()){
		dojo.xhrPost({
			url: '/service/doLogin',
			handleAs: 'json',
			handle: function(data,args){
				if(data.message == "success"){
					user = new selfpass.module.User(
						data.name, data.password, data.nickName);
					showMain();
				}else{
					showErrMsg("Wrong name or password. Login again ,please!");
					dojo.byId("errMsg2").innerHTML = errMsg;
				}
			},
			form: 'loginForm'
	  });
	}else{
		 showErrMsg("Please check again, some value you want to submit is incorrect");
	}
}
//Form Handler : [submit : new catalog]
function newCatalog(){
	if(dijit.byId("catalogForm").validate()){
		dojo.xhrPost({
				url: "/service/newCatalog",
				form: "catalogForm",
				handleAs: "json",
				handle: function(data,args){
					if(data.message && data.message=="success"){
						showSuccessMsg("New Catalog successly!");
						showTree();
					}else{
						showErrMsg("New Catalog Failure!");
					}
				}
		});
	}else{
		 showErrMsg("Please check again, some value you want to submit is incorrect");
	}
}
//Form Handler : [submit : modify catalog]
function modifyCatalog(){
	if(dijit.byId("catalogForm").validate()){
		dojo.xhrPost({
				url: "/service/modifyCatalog",
				form: "catalogForm",
				handleAs: "json",
				handle: function(data,args){
					if(data.message && data.message=="success"){
						showSuccessMsg("Modifies have been updated successly!");
						showTree();
					}else{
						showErrMsg("Modify Failure!");
					}
				}
		});
	}else{
		 showErrMsg("Please check again, some value you want to submit is incorrect");
	}
}
//Form Handler : [submit : new account]
function newAccount(){
	if(dijit.byId("accountForm").validate()){
		dojo.xhrPost({
				url: "/service/newAccount",
				handleAs: "json",
				handle: function(data,args){
					if(data.message && data.message=="success"){
						showSuccessMsg("New Account successly!");
						showAccountList(currentRootCatalogKey);
					}else{
						showErrMsg("New Account Failure!");
					}
				},
				content:{
					"account_catalog_key":dojo.byId("account_catalog_key").value,
					"account_name":dojo.byId("account_name").value,
					"account_id":dojo.byId("account_id").value,
					"account_pwd":encrypt(dojo.byId("account_pwd").value), //encrypt pwd
					"account_url":dojo.byId("account_url").value,
					"account_des":dojo.byId("account_des").value
				}
		});
	}
	else{
		 showErrMsg("Please check again, some value you want to submit is incorrect");
	}
}

//Form Handler : [submit : modify account]
function modifyAccount(){
	if(dijit.byId("accountForm").validate()){
			dojo.xhrPost({
				url: "/service/modifyAccount",
				handleAs: "json",
				handle: function(data,args){
					if(data.message && data.message=="success"){
						showSuccessMsg("Modifies have been updated successly!");
						showAccountList(currentRootCatalogKey);
					}else{
						showErrMsg("Modify Failure!");
					}
				},
				content:{
					"account_catalog_key":dojo.byId("account_catalog_key").value,
					"account_name":dojo.byId("account_name").value,
					"account_key":dojo.byId("account_key").value,
					"account_id":dojo.byId("account_id").value,
					"account_pwd":encrypt(dojo.byId("account_pwd").value), //encrypt pwd
					"account_url":dojo.byId("account_url").value,
					"account_des":dojo.byId("account_des").value
				}
		    });
	}
	else{
		 showErrMsg("Please check again, some value you want to submit is incorrect");
	}
}
function showSuccessMsg(msg){
	dojo.byId("submit_result_msg").innerHTML= msg;
	dojo.byId("submit_result_msg").className="successMsg msg";
}
function showErrMsg(msg){
	dojo.byId("submit_result_msg").innerHTML= msg;
	dojo.byId("submit_result_msg").className="errMsg msg";
}
